<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :module="form" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width: 80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
          <el-option v-for="item in list" :key="item.id" :value="item.manager" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width: 80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeess } from '@/api/employees'
import { addDepartments, getDepartmentDetail } from '@/api/departments'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义校验规则 验证部门编码对否一样
    const validCode = (rule, value, callback) => {
      // 验证用户输入的value值是否在部门编码中存在
      // 把 部门对象列表里的code单独拿出来组成一个新的数组
      let existCodeList = this.originList.map(item => item.code)
      // 如果是编辑状态,将当前编辑的部门的编码从存在的数组中移除,然后再判断
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (existCodeList.includes(value)) {
        // 包含: 失败
        // 抛出错误
        callback(new Error(`该部门编码${value}已存在`))
      } else {
        callback()
      }
    }
    // 自定义校验规则 验证部门名称是否和以及部门相同
    const validName = (rule, value, callback) => {
      // 被占用列表 也就是已经有的列表
      let existNameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      // 编辑状态下的部门名称不能与同级的兄弟部门的名称相同
      //
      //
      // 进入编辑状态
      if (this.isEdit) {
        // 通过当前部门的id 获取到当前部门的pi
        // 数组里面找对象就用find
        const pid = this.originList.find(item => item.id === this.id).pid
        // 拿到了当前部门的pid 过滤出除了当前部门之外的所有pid
        const brothers = this.originList.filter(item => item.pid === pid)// 此时也包含自己本身 所以要去掉自己
        // 排除掉自己
        existNameList = brothers.filter(item => item.pid !== this.pid)
      }

      if (existNameList.incudes(value)) {
        callback(new Error(`该部门名称${value}已经存在,不能重复`))
      } else {
        callback()
      }
    }
    return {
      list: [], // 储存部门负责人列表
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, trigger: 'blur' },
          // 自定义校验规则
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.laodEmployeesSimples()
    // 只有是编辑操作才需要请求详情数据
    if (this.isEdit) {
      this.loadDepartmentDetail()
    }
  },
  methods: {
    async loadDepartmentDetail() {
      try {
        const res = await getDepartmentDetail(this.id)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    async laodEmployeesSimples() {
      try {
        const { data: res } = await getEmployeess()
        console.log('111', res)
        res.depts.shift()
        this.list = res.depts
      } catch (error) {
        console.log(error)
      }
    },
    async doAdd() {
      // this.form中缺少pid
      try {
        const res = await addDepartments({ pid: this.id, ...this.form })
        console.log('新增部门', res)
        // 重新渲染
        // 关闭弹层
        // 子传父
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        // this.doAdd();
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消按钮
    hCancel() {
      // 通知父组件关闭
      this.$emit('close')
    }
  }
}
</script>

<style lang=""></style>
